<?php
namespace modules\post\service;

use idea\saf\sl\Service;
use idea\ap\UserIdentity;
use helper\ErrorNo;
use helper\ErrorMsg;
use helper\Util;
use helper\Log;
use modules\post\form;

/**
 * Posts class file
 * posts表服务类
 * @author 宋欢 <iphper@yeah.net>
 * @version $Id: Posts.php 1 2013-05-18 14:58:59Z huan.song $
 * @package modules.post.service
 * @since 1.0
 */
class Posts extends Service
{
	/**
     * @var integer 每页默认展示的行数
     */
    protected $_listRows = 4;

    /**
     * @var integer 每页默认展示的页码数
     */
    protected $_listPages = 4;

	/**
	 * 添加一条记录
	 * @param array $params
	 * @return array
	 */
	public function add(array $params = array())
	{
		$params['dt_modify'] = date('Y-m-d H:i:s');
		$params['admin_id'] = $params['modify_id'] = UserIdentity::getId();
		if (isset($params['post_flags']) && is_array($params['post_flags'])) {
			$params['post_flags'] = implode(',', $params['post_flags']);
		}

		return $this->insert($params, $this->getForm()->getRules());
	}

	/**
	 * 更新一条记录
	 * @param string|integer $id
	 * @param array $params
	 * @return array
	 */
	public function edit($id , array $params = array())
	{
		$params['dt_modify'] = date('Y-m-d H:i:s');
		$params['modify_id'] = UserIdentity::getId();
		if (isset($params['post_flags']) && is_array($params['post_flags'])) {
			$params['post_flags'] = implode(',', $params['post_flags']);
		}

		if (strpos($id, ',') !== false) {
			return $this->batchUpdateByPk($id, $params, $this->getForm()->getRules());
		}

		return $this->updateByPk($id, $params, $this->getForm()->getRules());
	}

	/**
	 * 更新是否激活的状态
	 * @param string|integer $id
	 * @param string $status
	 * @return array
	 */
	public function editStatus($id, $status = '')
	{
		$params = array(
			'post_status' => ($status === '') ? form\Posts::POST_STATUS_T : strtolower($status)
		);

		return $this->edit($id, $params);
	}

	/**
	 * 查询文档
	 * @param array $params
	 * @param string $order
	 * @return array
	 */
	public function search(array $params = array(), $order = '')
	{
		$condition = '1';
		$attributes = array();

		if (isset($params['category_id']) && ($id = (int) $params['category_id']) > 0) {
			$condition .= ' AND category_id = ?';
			$attributes['category_id'] = $id;
		}

		if (isset($params['post_flags']) && isset(form\Posts::$flags[$params['post_flags']])) {
			$condition .= ' AND FIND_IN_SET(?, post_flags)';
			$attributes['post_flags'] = $params['post_flags'];
		}

		if (isset($params['post_status']) && isset(form\Posts::$statuses[$params['post_status']])) {
			$condition .= ' AND post_status = ?';
			$attributes['post_status'] = $params['post_status'];
		}

		$url = Util::getUrlByAct(array_merge($attributes, array('order_by' => $order)));
		return $this->findListByCondition($condition, $attributes, $order, $url);
	}

	/**
	 * (non-PHPdoc)
	 * @see idea\saf\sl.Service::findByPk()
	 */
	public function findByPk($value)
	{
		$ret = parent::findByPk($value);
		if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) {
			return $ret;
		}
		
		$ret['data']['post_flags'] = explode(',', $ret['data']['post_flags']);
		return $ret;
	}

	/**
	 * 通过类别ID获取该类别下文档数
	 * @param integer $value
	 * @return array
	 */
	public function countByCid($value) 
	{
		return $this->countByAttributes(array(
			'category_id' => $value
		));
	}
}
